package com.rrd.mex.dw.mr.babel.parser;

import com.rrd.mex.dw.utils.DateUtil;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.hfq.UploadBaseData;
import com.ucredit.babel.common.model.hfq.plist.UploadAppDetail;
import com.ucredit.babel.common.model.hfq.plist.UploadPlistData;
import com.ucredit.babel.common.model.hfq.plist.UploadPlistRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * APPlist
 *
 * @author guoqiang
 */

public class MexPlistRecordParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * APPlist
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        UploadPlistRecord obj = (UploadPlistRecord) model;
        UploadPlistData data = obj.getData() == null ? new UploadPlistData() : obj.getData();
        StringBuilder sb = getBaseInfo(obj, successful);
        UploadBaseData baseData = data.getBaseData();
        sb.append(baseData.getHttpTimestamp() == 0 ? "" : DateUtil.getUTC6(baseData.getHttpTimestamp())).append(CRT001);
        sb.append(baseData.getUploadInfoOpportunity() == null ? "" : baseData.getUploadInfoOpportunity()).append(CRT001);;
        sb.append(baseData.getVersion() == null ? "" : baseData.getVersion()).append(CRT001);
        sb.append(baseData.getIsAuthorized() == null ? "" : baseData.getIsAuthorized()).append(CRT001);
        sb.append(baseData.getOs() == null ? "" : baseData.getOs()).append(CRT001);

        List<UploadAppDetail> appDetails = data.getAppList();
        setFileNum(10);
        StringBuilder TxSb = new StringBuilder(500);
        for (UploadAppDetail app : appDetails) {
            TxSb.delete(0, TxSb.length());
            TxSb.append(app.getPackageName() == null ? "" : app.getPackageName()).append(CRT001);
            TxSb.append(app.getAppName() == null ? "" : app.getAppName()).append(CRT001);
            TxSb.append(app.getLastOpenTime() == null ? "" : DateUtil.getUTC6(app.getLastOpenTime().getTime())).append(CRT001);
            TxSb.append(app.getLastUpdateTime() == null ? "" : DateUtil.getUTC6(app.getLastUpdateTime().getTime())).append(CRT001);
            key.set(getKey());
            val.set((sb.toString() + TxSb).replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }
        if (appDetails.size() <= 0) {
            key.set(getKey());
            val.set(sb.toString().replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_mex_plist_record_inc";
    }

    @Override
    public String getType() {
        return RecordType.HFQ_UPLOAD_PLIST_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "APPlist";
    }
    @Override
    public BaseModel getModel() {
        UploadPlistRecord plist = new UploadPlistRecord();
        return plist;
    }

}
